/**
 * convertTothousandths的选项
 * @interface IConvertTothousandthsOptions
 */
interface IConvertTothousandthsOptions {
  /** 分隔符 */
  separator?: string;
}

/**
 *
 * 将数字字符串转化为千分位
 * @export
 * @since 1.0.0
 * @param {(number | string)} [num='']
 * @param {IConvertTothousandthsOptions} [options={}]
 * @param {string} [options.separator=','] 分隔符
 * @return {*}  {string}
 */
export default function convertTothousandths(
  num: number | string = '',
  { separator = ',' }:IConvertTothousandthsOptions = {},
): string {
  // 参数检查
  if (typeof num !== 'number' && typeof num !== 'string') {
    return '';
  }
  // 转换为字符串
  let newNum = `${num}`;
  if (!newNum.includes('.')) {
    newNum += '.';
  }
  // @ts-ignore
  return newNum.replace(/(\d)(?=(\d{3})+\.)/g, ($0, $1) => `${$1}${separator}`).replace(/\.$/, '');
}
